package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.sun.java.accessibility.util.java.awt.ListTranslator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.List;

/**
 * 套餐管理
 */

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Reference
    private SetmealService setmealService;
    @Autowired
    //创建jedisPool对象，操作redis。
    private JedisPool jedisPool;

    //查询所有套餐
    @RequestMapping("/getAllSetmeal")
    public Result getAllSetmeal() {
        List<Setmeal> list;
        try {
            ObjectMapper mapper = new ObjectMapper();
            //转换json数据为list集合
            String json = jedisPool.getResource().get(RedisConstant.SETMEAL_LIST);
            if (json != null && json.length() > 0) {
                JavaType javaType = mapper.getTypeFactory().constructParametricType(ArrayList.class, Setmeal.class);
                list = mapper.readValue(json, javaType);
            } else {
                list = setmealService.findAll();
                //转换list集合为json数据
                String value = mapper.writeValueAsString(list);
                //存入jedis
                jedisPool.getResource().set(RedisConstant.SETMEAL_LIST, value);
            }
            return new Result(true, MessageConstant.GET_SETMEAL_LIST_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_SETMEAL_LIST_FAIL);
        }
    }

    //根据套餐ID查询套餐详情（套餐基本信息、套餐对应的检查组信息、检查组对应的检查项信息）
    @RequestMapping("/findById")
    public Result findById(int id) {
        ObjectMapper mapper = new ObjectMapper();
        Setmeal setmeal = new Setmeal();
        try {
            String key = jedisPool.getResource().get(RedisConstant.SETMEAL_ALL + id);
            if (key != null && key.length() > 0) {
                setmeal = mapper.readValue(key, setmeal.getClass());
                System.out.println("use redis");
            } else {
                setmeal = setmealService.findById(id);
                String setmealId = mapper.writeValueAsString(setmeal);
                jedisPool.getResource().set(RedisConstant.SETMEAL_ALL + id, setmealId);
                System.out.println("use dao");
            }
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }


}
